<template>
	<view>
		<form>
			<view class="cu-form-group margin-top">
				<view class="title">姓名</view>
				<input placeholder="您的姓名" name="input" v-model="m.nickName"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">手机号码</view>
				<input placeholder="" v-model="m.mobile" disabled="disabled" name="input"></input>
			</view>
			<view class="cu-form-group" style="padding:10px;">
				<view class="grid col-4 grid-square flex-sub">
					<view class="bg-img" v-for="(item,index) in imgList" :key="index" @tap="ViewImage" :data-url="imgList[index]">
					 <image :src="imgList[index]" mode="aspectFill"></image>
						<view class="cu-tag bg-red" @tap.stop="DelImg" :data-index="index">
							<text class='cuIcon-close'></text>
						</view>
					</view>
					<view class="solids" @tap="ChooseImage" v-if="imgList.length<1">
						<text class='cuIcon-cameraadd'></text>
					</view>
				</view>
			</view>
			<view class="padding">
				<button class="cu-btn block margin-tb-sm lg margin-top round bg-yellow" @click="saves">保存</button>
			</view>
		</form>
	</view>
</template>

<script>
	import {
	    mapState,
		mapMutations
	} from 'vuex';  
	export default {
		data() {
			return {
				m:{},
				imgList: [],
				isload:false
			}
		},
		computed: {
			...mapState(['hasLogin','user'])
		},
		onLoad() {
			this.m=this.user;
		},
		methods: {
			...mapMutations(['login']),
			saves(){
				if(!this.m.nickName){
					this.$api.msg('请输入姓名');
					return;
				}
				uni.showLoading({
					title: 'loading'
				});
				this.user.nickName=this.m.nickName;
				let _this=this;
				this.$api.ajax('index/update', this.user, function(res) {
					uni.hideLoading();
					var data = res[1].data; console.log(data);
					if (data.statusCode == 200) {
						//如果有图片，开始上传图片
						//_this.$api.msg('图片长度'+_this.imgList.length);
						if(_this.imgList.length>0){
							for(let i=0; i<_this.imgList.length; i++){
								//console.log({guid:_this.user.guid,type:'1'});
								//console.log(_this.imgList[i]);
								_this.uploadImg(_this.imgList[i],{guid:_this.user.guid,type:'1'})
							}
						}else{
							_this.login(_this.user);
							setTimeout(()=>{
								uni.navigateBack();
							}, 500)
						}
						
					} else {
						_this.$api.msg('网络失败');
					}
				});
			},
			uploadImg(img,data){
				//this.$api.msg('上传A');
				let that=this;console.log(data);
				uni.uploadFile({
					url: that.$url+'/api/app/files/upload', 
					filePath: img,
					name: 'file',
					formData: data,
					success: (uploadFileRes) => {
						var result=JSON.parse(uploadFileRes.data);
						//that.$api.msg(result.data);
						that.user.headPic=result.data;//console.log(result);
						that.login(that.user);
						setTimeout(()=>{
							uni.navigateBack();
						}, 500)
					}
				});
			},
			ChooseImage() {
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					//sourceType: ['album'], //从相册选择
					success: (res) => {
						if (this.imgList.length != 0) {
							this.imgList = this.imgList.concat(res.tempFilePaths)
						} else {
							this.imgList = res.tempFilePaths
						}
					}
				});
			},
			ViewImage(e) {
				uni.previewImage({
					urls: this.imgList,
					current: e.currentTarget.dataset.url
				});
			},
			DelImg(e) {
				uni.showModal({
					title: '提示',
					content: '确定要删除这张图片吗？',
					cancelText: '取消',
					confirmText: '是的',
					success: res => {
						if (res.confirm) {
							this.imgList.splice(e.currentTarget.dataset.index, 1)
						}
					}
				})
			}
		}
	}
</script>

<style lang='scss'>
	page {
		background-color: #ffffff;
	}
</style>
